#include "Tcp.h"
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <sys/epoll.h>
using namespace std;
using namespace PFS;

void* fun(void* arg)
{
    Tcp* tcp = (Tcp *)arg;
//    TcpServer* server = ((struct Arg *)arg)->server;
    int len;
    int ret;
/*    struct epoll_event ev;
    struct event evt;
    evt.fd = tcp->get_fd();
    evt.tcp = tcp;
    ev.events = EPOLLIN | EPOLLET;
    ev.data.ptr = &evt; 
    cout<<"fun fd "<<tcp->get_fd()<<" epoll_hanlder"<<server->get_epoll_handler()<<endl;
  */
    ret = tcp->Recv(&len,sizeof(int));
    if(ret < 0)
        pthread_exit((void *)-1);
    char* buf;
    buf = (char *)calloc(len + 1, sizeof(char));
    tcp->Recv(buf,len);
    buf[len] = 0;
    cout<<"recv "<<len<<" Bytes, content is: "<<buf<<endl;
    delete buf;
//    epoll_ctl(server->get_epoll_handler(),EPOLL_CTL_MOD, tcp->get_fd(), &ev);
    cout<<"return\n";
    pthread_exit((void *)len);
}

int main(int argc,char **argv)
{
    if(argc != 2)
    {
        cout<<"Usage:"<<argv[0]<<" port"<<endl;
        exit(1);
    }
    cout<<"1"<<endl;
    TcpServer server(atoi(argv[1]));
    cout<<"2"<<endl;
    server.set_connect_handle(fun);
    cout<<"3"<<endl;
    server.start();
    return 0;
}
